package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.mapper.LinkMapper;
import com.cdd.index.service.ILinkService;
import com.common.entity.Link;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-06
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements ILinkService {

    @Autowired
    private LinkMapper linkMapper;
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, Link link) {
        //System.out.println("-------------------------"+link);
        Page<Link> page =  new Page<>(currentPage,pageSize);
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(link.getLinkName())){
            wrapper.like("link_name",link.getLinkName());
        }
        if (link.getIsDisable()!=null){
            wrapper.eq("is_disable",link.getIsDisable());
        }
        Page<Link> linkPage = linkMapper.selectPage(page, wrapper);
       // System.out.println("-------------------------"+link);
        return new CommonResult(2000,"查询成功",linkPage);
    }

    @Override
    public CommonResult findAll() {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<Link> linkList = linkMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",linkList);
    }

    @Override
    public CommonResult updateStatus(String linkId, Integer isDisable) {
        Link link = new Link();
        link.setLinkId(linkId);
        link.setIsDisable(isDisable);
        linkMapper.updateById(link);
        return new CommonResult(2000,"修改状态成功",null);
    }

    @Override
    public CommonResult linkAdd(Link link) {
        link.setIsDisable(0);
        link.setIsDeleted(0);
        linkMapper.insert(link);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult updateLink(Link link) {
        linkMapper.updateById(link);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult deleteLink(String linkId) {
        linkMapper.deleteById(linkId);
        return new CommonResult(2000,"删除成功",null);
    }

}
